//
//  UCBuyHistoryViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddUCBuyHistoryViewController.h"
#import "JddCommonFunction.h"
#import "JddViewControllerManager.h"
#import "SBJson.h"


@interface JddUCBuyHistoryViewController ()

@end

@implementation JddUCBuyHistoryViewController

@synthesize ListTable;
@synthesize prev;
@synthesize tipLable;
@synthesize next;
@synthesize seg;
@synthesize LotID;
@synthesize titleScorllView;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddUCBuyHistoryViewController);


-(IBAction)selectNext:(id)sender{
    if(hasNext){
        if (buyDataResult.count < 10) {
            pageNo = 0;
            [buyDataResult removeAllObjects];
        }
        pageNo ++;
        [self startQueryOperation];
    }
}

-(IBAction)selectPrev:(id)sender{
    if (hasPrev) {
        pageNo --;
        [self startQueryOperation];
    }
    
}


-(void)startQueryOperation{
//    seg.enabled = NO;
    [[JddGlobalParameter sharedInstance].operationQueue cancelAllOperations];
    JddQueryBuyHistoryOperation *operation = [[JddQueryBuyHistoryOperation alloc] init];
    operation.PageNo = [NSNumber numberWithInt:pageNo];
    operation.PageSize = [NSNumber numberWithInt:pageSize];
    operation.LotID = [NSString stringWithFormat:@"%d", LotID];
    operation.delegate  = self;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

-(IBAction)selectSegment:(id)sender{
    UISegmentedControl *segValue = sender;
    switch (segValue.selectedSegmentIndex) {
        case 0:
            LotID = 0;
            break;
        case 1:
            LotID = LOTTERY_TYPE_ID_11S5;
            break;
        case 2:
            LotID = LOTTERY_TYPE_ID_11YDJ;
            break;
        case 3:
            LotID = LOTTERY_TYPE_ID_CQSS;
            break;
        case 4:
            LotID = LOTTERY_TYPE_ID_FOOTBALL;
            break;
        case 5:
            LotID = LOTTERY_TYPE_ID_BASKETBALL;
            break;
        case 6:
            LotID = LOTTERY_TYPE_ID_FC3D;
            break;
            
//        case 0:
//            LotID = 0;
//            break;
//        case 1:
//            LotID = LOTTERY_TYPE_ID_SHUANGSEQIU;
//            break;
//        case 2:
//            LotID = LOTTERY_TYPE_ID_DALETOU;
//            break;
//        case 3:
//            LotID = LOTTERY_TYPE_ID_11S5;
//            break;
//        case 4:
//            LotID = LOTTERY_TYPE_ID_FOOTBALL;
//            break;
////        case 5:
////            LotID = LOTTERY_TYPE_ID_BD;
////            break;
//        case 5:
//            LotID = LOTTERY_TYPE_ID_BASKETBALL;
//            break;
//        case 6:
//            LotID = LOTTERY_TYPE_ID_SF;
//            break;
//        case 7:
//            LotID = LOTTERY_TYPE_ID_RX9;
//            break;
//        case 8:
//            LotID = LOTTERY_TYPE_ID_FC3D;
//            break;
//        case 9:
//            LotID = LOTTERY_TYPE_ID_7LC;
//            break;
//        case 10:
//            LotID = LOTTERY_TYPE_ID_PL3;
//            break;
//        case 11:
//            LotID = LOTTERY_TYPE_ID_PL5;
//            break;
//        case 12:
//            LotID = LOTTERY_TYPE_ID_7XC;
//            break;
//        case 13:
//            LotID = LOTTERY_TYPE_ID_11YDJ;
//            break;
////        case 15:
////            LotID = LOTTERY_TYPE_ID_CQSS;
////            break;
        default:
            break;
    }
    [self selectSegmentPanel];
    pageNo = 1;
    [self startQueryOperation];
}


-(void)selectCell:(NSString*)schemeId :(NSString*)DataSource{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    NSString* daid = [NSString stringWithFormat:@"%@",DataSource];
    if([daid isEqualToString:@"0"]){
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        JddOldQueryBuyHistoryDetailOperation *operation = [[JddOldQueryBuyHistoryDetailOperation alloc] init];
        operation.SchemeId = [NSString stringWithFormat:@"%d",[schemeId intValue]];
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }else{
        JddQueryBuyHistoryDetailOperation *operation = [[JddQueryBuyHistoryDetailOperation alloc] init];
        operation.SchemeId = schemeId;
        operation.DataSource = DataSource;
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
   
}

-(void)QueryBuyHistoryOperationResult:(NSData*)result{
    if (result) {
        if ([JddDataParser parseModifyPasswordResult:result]) {
            SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
            NSDictionary *dictResult = [jsonParser objectWithData:result];
            NSMutableDictionary *data = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            NSMutableArray *sportListString = [data objectForKey:@"item"];
            
            if (pageNo>1) {
                for (NSMutableArray *sport in sportListString) {
                    [buyDataResult addObject:sport];
                }
            }else {
                buyDataResult = sportListString;
                if ([buyDataResult count]>0) {
                    tipLable.hidden = YES;
                }else {
                    tipLable.hidden = NO;
                }
            }   
            if ([[data objectForKey:@"Count"] intValue] == [buyDataResult count]) {
                next.hidden = YES;
                hasNext = NO;
            }else {
                next.hidden = NO;
                hasNext = YES;
            }
            
            [ListTable reloadData];
        } else {
            NSLog(@"投注记录调试信息：%@",[JddError sharedInstance].message);
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }
}

-(void)OldQueryBuyHistoryDetailOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        resultString = [JddDataParser oldparseBuyHistoryDetailResult:result];
        if (resultString == nil) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BUY_DETAIL_DIGITAL_OLD
                                                             fromController:self
                                                                 withObject:resultString
                                                                withObject2:nil];
        
    }
}

-(void)QueryBuyHistoryDetailOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        resultString = [JddDataParser parseBuyHistoryDetailResult:result];
        if (resultString == nil) {
             [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BUY_DETAIL_DIGITAL 
                                                             fromController:self 
                                                                 withObject:resultString 
                                                                withObject2:nil]; 
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [buyDataResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   JddBuyHistorySSQCell* cell = [JddBuyHistorySSQCell getInstanceView];
    NSDictionary *cellData = [buyDataResult objectAtIndex:indexPath.row];
    cell.typeLabel.text = [cellData objectForKey:@"LotName"];
    cell.issueLabel.text =[NSString stringWithFormat:@"%@期", [cellData objectForKey:@"Issue"]];
    cell.moneyLabel.text = [NSString stringWithFormat:@"%d彩豆", [[cellData objectForKey:@"Money"] intValue]];
    NSString *string1 = [cellData objectForKey:@"WinMoney"];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]+)$"];
    if ([pred evaluateWithObject:string1]) {
        cell.getMoneyLabel.text =[NSString stringWithFormat:@"%3.1f彩豆",[[cellData objectForKey:@"WinMoney"] floatValue]];
    }else {
        cell.getMoneyLabel.text = [cellData objectForKey:@"WinMoney"];
    }
    cell.stateLabel.text = [cellData objectForKey:@"State"];
    cell.schemeId = [cellData objectForKey:@"ID"];
    cell.DataSource = [cellData objectForKey:@"DataSource"];
    cell.delegate = self;
    return cell;
}


-(void)destoryView{
    if ([buyDataResult count]>0) {
        [buyDataResult removeAllObjects];
    }    
    [ListTable reloadData];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   // [self.navigationItem setTitle:PAGE_BUY_HISTORY];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_BUY_HISTORY];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
        //self.navigationItem.hidesBackButton = YES;
    pageNo = 1;
    pageSize = 10;
    next.hidden = YES;
    prev.hidden = YES;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.ListTable.tableFooterView = v;
    [ListTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [ListTable setSeparatorInset:UIEdgeInsetsZero];
    }
}

-(void)selectSegmentPanel
{
    NSInteger index = 0;
    switch (LotID) {
        case 0:
            index = 0;
            break;
        case LOTTERY_TYPE_ID_11S5:
            index = 1;
            break;
        case LOTTERY_TYPE_ID_11YDJ:
            index = 2;
            break;
        case LOTTERY_TYPE_ID_CQSS:
            index = 3;
            break;
        case LOTTERY_TYPE_ID_FOOTBALL:
            index = 4;
            break;
        case LOTTERY_TYPE_ID_BASKETBALL:
            index = 5;
            break;
        case LOTTERY_TYPE_ID_FC3D:
            index = 6;
            break;
            
//        case LOTTERY_TYPE_ID_SHUANGSEQIU:
//            index = 1;
//            break;
//        case LOTTERY_TYPE_ID_DALETOU:
//            index = 2;
//            break;
//        case LOTTERY_TYPE_ID_11S5:
//            index = 3;
//            break;
//        case LOTTERY_TYPE_ID_FOOTBALL:
//            index = 4;
//            break;
////        case LOTTERY_TYPE_ID_BD:
////            index = 5;
////            break;
//        case LOTTERY_TYPE_ID_BASKETBALL:
//            index = 5;
//            break;
//        case LOTTERY_TYPE_ID_SF:
//            index = 6;
//            break;
//        case LOTTERY_TYPE_ID_RX9:
//            index = 7;
//            break;
//        case LOTTERY_TYPE_ID_FC3D:
//            index = 8;
//            break;
//        case LOTTERY_TYPE_ID_7LC:
//            index = 9;
//            break;
//        case LOTTERY_TYPE_ID_PL3:
//            index = 10;
//            break;
//        case LOTTERY_TYPE_ID_PL5:
//            index = 11;
//            break;
//        case LOTTERY_TYPE_ID_7XC:
//            index = 12;
//            break;
//        case LOTTERY_TYPE_ID_11YDJ:
//            index = 13;
//            break;
////        case LOTTERY_TYPE_ID_CQSS:
////            index = 15;
////            break;
        default:
            index = 0;
            break;
    }
    CGPoint pt = CGPointZero;
    if (index > 2) {
        CGRect rc = [seg bounds];
        rc = [seg convertRect:rc toView:self.titleScorllView];
        pt = rc.origin;
        float number = index;
        if (number >= 4) {//(number >= 13) {
            number = 4;//number = 13.4;
        }
        pt.x += (number -2)*(seg.frame.size.width/seg.numberOfSegments);
        pt.y = 0;
        [self.titleScorllView setContentOffset:pt animated:YES];
    }else{
        [self.titleScorllView setContentOffset:pt animated:YES];        
    }
    [seg setSelectedSegmentIndex:index];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"GouCaiJiLu"];
    [MobClick event:@"UserCenter-touzhujilu"];
    pageNo = 1;
    pageSize = 10;
    tipLable.hidden = YES;
    buyDataResult = [[NSMutableArray alloc] init];
    [ListTable reloadData];
    [self selectSegmentPanel];
    
    
    [self startQueryOperation];
    titleScorllView.contentSize = CGSizeMake(seg.frame.size.width, seg.frame.size.height);
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"GouCaiJiLu"];
}
- (void)viewDidUnload
{
    [self setTitleScorllView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
